安卓保活 您所在的位置:网站首页 android 进程保活 安卓保活

安卓保活

#安卓保活| 来源: 网络整理| 查看: 265

做Android 保活的背景

由于之前做一个项目的时候需要让进程一直在后台活下去保持不被杀死的状态,因此也是各种百度各种苦苦寻找,本来是想着靠Service来做保活的,因为我的手机是6.0系统的,之前试过的各种依靠Service的方式均以失败告终,因此决定站在另一个角度上来解决问题,–>Android的进程。

方案一:双进程守护

其实诸如类似360杀毒软件之类的产品本身原理是通过一个一个的遍历进程,如果存活就杀死从而达到清理软件的作用的,所以我们是可以拿到自己进程和创建新的进程的。而通过AIDL的接口则可以实现跨进程通信,因此,使用双进程并通过进程间的通信是一种可行的解决方案。因此方案一是通过双进程守护来解决这个Android应用保活的。

首先是一个AIDL接口,两边的Service都要通过继承Service_1.Stub来实现AIDL接口中的方法,这里做一个空实现,目的是为了实现进程通信。接口声明如下:

packagecom.ph.myservice;interfaceService_1 {

String getName();

}

然后是两个Service,为了保持连接,内部写一个内部类实现ServiceConnection的接口,当外部杀了其中一个进程的时候,会进入onDisConnection中,那么此时要做的就是start和bind另一个进程,因为Service的启动是可以多次的,所以这样是没问题的,代码如下:

packagecom.ph.myservice;importandroid.app.ActivityManager;importandroid.app.ActivityManager.RunningServiceInfo;importandroid.app.Service;importandroid.content.ComponentName;importandroid.content.Context;importandroid.content.Intent;importandroid.content.ServiceConnection;importandroid.os.IBinder;importandroid.os.RemoteException;importandroid.widget.Toast;importjava.util.List;public class LocalService extendsService {privateServiceConnection conn;privateMyService myService;

@OverridepublicIBinder onBind(Intent intent) {returnmyService;

}

@Overridepublic voidonCreate() {super.onCreate();

init();

}private voidinit() {if (conn == null) {

conn= newMyServiceConnection();

}

myService= newMyService();

}

@Overridepublic int onStartCommand(Intent intent, int flags, intstartId) {

Toast.makeText(getApplicationContext(),"本地进程启动", Toast.LENGTH_LONG).show();

Intent intents= newIntent();

intents.setClass(this, RemoteService.class);

bindService(intents, conn, Context.BIND_IMPORTANT);returnSTART_STICKY;

}class MyService extendsService_1.Stub {

@Overridepublic String getName() throwsRemoteException {return null;

}

}class MyServiceConnection implementsServiceConnection {

@Overridepublic voidonServiceConnected(ComponentName name, IBinder service) {

System.out.println("获取连接");

}

@Overridepublic voidonServiceDisconnected(ComponentName name) {

Toast.makeText(LocalService.this, "远程连接被干掉了", Toast.LENGTH_SHORT).show();

LocalService.this.startService(new Intent(LocalService.this,

RemoteService.class));

LocalService.this.bindService(new Intent(LocalService.this,

RemoteService.class), conn, Context.BIND_IMPORTANT);

}

}

}

远程服务类如下:

packagecom.ph.myservice;importandroid.app.Service;importandroid.content.ComponentName;importandroid.content.Context;importandroid.content.Intent;importandroid.content.ServiceConnection;importandroid.os.IBinder;importandroid.os.RemoteException;importandroid.widget.Toast;public class RemoteService extendsService {privateMyBinder binder;privateServiceConnection conn;

@Overridepublic voidonCreate() {super.onCreate();//System.out.println("远程进程开启");

init();

}

@Overridepublic int onStartCommand(Intent intent, int flags, intstartId) {

Toast.makeText(getApplicationContext(),"远程进程启动", Toast.LENGTH_LONG).show();

Intent intents= newIntent();

intents.setClass(this, LocalService.class);

bindService(intents, conn, Context.BIND_IMPORTANT);returnSTART_STICKY;

}private voidinit() {if (conn == null) {

conn= newMyConnection();

}

binder= newMyBinder();

}

@OverridepublicIBinder onBind(Intent intent) {returnbinder;

}static class MyBinder extendsService_1.Stub {

@Overridepublic String getName() throwsRemoteException {return "远程连接";

}

}class MyConnection implementsServiceConnection {

@Overridepublic voidonServiceConnected(ComponentName name, IBinder service) {

System.out.println("获取远程连接");

}

@Overridepublic voidonServiceDisconnected(ComponentName nme) {

Toast.makeText(RemoteService.this, "本地连接被干掉了", Toast.LENGTH_SHORT).show();

RemoteService.this.startService(new Intent(RemoteService.this,

LocalService.class));

RemoteService.this.bindService(new Intent(RemoteService.this,

LocalService.class), conn, Context.BIND_IMPORTANT);

}

}

}

布局文件里要加上声明

实际情况我个人测试,在5.0以下的模拟器上是没问题的,不管多次从系统的进程里kill掉,也还是会重新启动tos,但是5.0以上这种方法是无效的,5.0以上Android应该是意识到了这种双进程守护的方式,因此修改了一下源码,让这种双进程保活应用的方式无效。因此,针对5.0以上,我们采用另一种方案。

方案二:JobScheduler执行任务调度保活

JobScheduler这个类是21版本google新出来的api,我们看他的文档可以知道大致这个类的作用如下:

框架将智能当你收到你的回调,并尝试批并尽可能推迟。通常如果你不指定期限在你的工作,它可以运行在任何时候根据作业调度器的当前状态的内部队列,然而它可能是延迟只要直到下一次设备被连接到一个电源。

这个任务其实是在设备空闲期执行的,而且系统设计的这个api不会很耗电,本意是用来执行一些任务调度的,但是我们设想一下,如果用这个类来执行我们的开启双进程,那么也是一定会在设备空闲期执行的,因此我们写一个类继承JobService,在onstart里声明创建JobScheduler对象,并设置多就执行一次和开机自启动,这样就能确保及时在息屏状态,也能够执行重启进程,所以我们在JobService的onStopJob方法里判断我们的进程是否被回收了,如果被回收了就重启进程,这样子就可以实现5.0以上的进程保活了。具体代码如下:

packagecom.ph.myservice;importandroid.annotation.SuppressLint;importandroid.app.ActivityManager;importandroid.app.job.JobInfo;importandroid.app.job.JobParameters;importandroid.app.job.JobScheduler;importandroid.app.job.JobService;importandroid.content.ComponentName;importandroid.content.Context;importandroid.content.Intent;importandroid.os.Build;importandroid.widget.Toast;importjava.util.List;/*** Created by 86119 on 2017/1/6.*/@SuppressLint("NewApi")public class JobHandlerService extendsJobService {privateJobScheduler mJobScheduler;

@Overridepublic int onStartCommand(Intent intent, int flags, intstartId) {

System.out.println("服务被创建");//startService(new Intent(this, LocalService.class));//startService(new Intent(this, RemoteService.class));

if (Build.VERSION.SDK_INT >=Build.VERSION_CODES.LOLLIPOP) {

mJobScheduler=(JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);

JobInfo.Builder builder= new JobInfo.Builder(startId++,new ComponentName(getPackageName(), JobHandlerService.class.getName()));

builder.setPeriodic(5000); //每隔5秒运行一次

builder.setRequiresCharging(true);

builder.setPersisted(true); //设置设备重启后,是否重新执行任务

builder.setRequiresDeviceIdle(true);if (mJobScheduler.schedule(builder.build())

Toast.makeText(this, "服务启动", Toast.LENGTH_SHORT).show();//|| isServiceRunning(this, "com.ph.myservice.RemoteService") == false

System.out.println("开始工作");//if (!isServiceRunning(getApplicationContext(), "com.ph.myservice") || !isServiceRunning(getApplicationContext(), "com.ph.myservice:remote")) {//startService(new Intent(this, LocalService.class));//startService(new Intent(this, RemoteService.class));//}

/*boolean serviceRunning = isServiceRunning(getApplicationContext(), "com.ph.myservice");

System.out.println("进程一" + serviceRunning);

boolean serviceRunning2 = isServiceRunning(getApplicationContext(), "com.ph.myservice:remote");

System.out.println("进程二" + serviceRunning2);*/

return false;

}

@Overridepublic booleanonStopJob(JobParameters params) {if (!isServiceRunning(this, "com.ph.myservice.LocalService") || !isServiceRunning(this, "com.ph.myservice.RemoteService")) {

startService(new Intent(this, LocalService.class));

startService(new Intent(this, RemoteService.class));

}return false;

}//服务是否运行

public booleanisServiceRunning(Context context, String serviceName) {boolean isRunning = false;

ActivityManager am= (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);

List lists =am.getRunningAppProcesses();for (ActivityManager.RunningAppProcessInfo info : lists) {//获取运行服务再启动

System.out.println(info.processName);if(info.processName.equals(serviceName)) {

isRunning= true;

}

}returnisRunning;

}

}packagecom.ph.myservice;importandroid.content.Intent;importandroid.os.Build;importandroid.os.Bundle;importandroid.support.v7.app.AppCompatActivity;public class MainActivity extendsAppCompatActivity {

@Overrideprotected voidonCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);if (Build.VERSION.SDK_INT >=Build.VERSION_CODES.LOLLIPOP) {

openJobService();

}else{

openTwoService();

}

}private voidopenTwoService() {

startService(new Intent(this, LocalService.class));

startService(new Intent(this, RemoteService.class));

}private voidopenJobService() {

Intent intent= newIntent();

intent.setClass(MainActivity.this, JobHandlerService.class);

startService(intent);

}

}

经过我6.0系统的华为真机测试是没有问题的,就算处于息屏状态进程也还是活着的,不管过多久打开屏幕还是会tos,并且关机了开机也会tos。以上就是我的Android保活的两种方案啦,其实还有更深入的几种方案,但是涉及到ndk层和Linux层我目前也没做,就不在这里说啦。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

      专题文章
        CopyRight 2018-2019 实验室设备网 版权所有